home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue36 / Imagelst / DragDropForm.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-04-05  |  1.7 KB  |  66 lines

  1. unit DragDropForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ComCtrls, ExtCtrls;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     DragImages: TImageList;
  12.     DragSource: TStaticText;
  13.     Target: TImage;
  14.     procedure DragSourceMouseDown(Sender: TObject; Button: TMouseButton;
  15.       Shift: TShiftState; X, Y: Integer);
  16.     procedure DragSourceMouseUp(Sender: TObject; Button: TMouseButton;
  17.       Shift: TShiftState; X, Y: Integer);
  18.     procedure DragSourceMouseMove(Sender: TObject; Shift: TShiftState; X,
  19.       Y: Integer);
  20.   private
  21.     ImageIndex: Integer;
  22.   end;
  23.  
  24. var
  25.   MainForm: TMainForm;
  26.  
  27. implementation
  28.  
  29. {$R *.DFM}
  30.  
  31. procedure TMainForm.DragSourceMouseDown(Sender: TObject;
  32.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  33. begin
  34.   Randomize;
  35.   ImageIndex := Random (DragImages.Count);
  36.   DragImages.SetDragImage (ImageIndex, 0, 0);
  37.   DragImages.DragCursor := crDefault;
  38.   DragImages.BeginDrag (Self.Handle, DragSource.Left + X, DragSource.Top + Y);
  39. end;
  40.  
  41. procedure TMainForm.DragSourceMouseUp(Sender: TObject; Button: TMouseButton;
  42.   Shift: TShiftState; X, Y: Integer);
  43. var
  44.   MousePos: TPoint;
  45.   TargetControl: TControl;
  46. begin
  47.   DragImages.EndDrag;
  48.   // Find out what's under the mouse.
  49.   MousePos.X := DragSource.Left + X;
  50.   MousePos.Y := DragSource.Top + Y;
  51.   TargetControl := Self.ControlAtPos (MousePos, True);
  52.   if TargetControl = Target then begin
  53.     DragImages.GetIcon (ImageIndex, Target.Picture.Icon);
  54.   end;
  55. end;
  56.  
  57. procedure TMainForm.DragSourceMouseMove(Sender: TObject; Shift: TShiftState;
  58.   X, Y: Integer);
  59. begin
  60.   if DragImages.Dragging then begin
  61.     DragImages.DragMove (DragSource.Left + X, DragSource.Top + Y);
  62.   end;
  63. end;
  64.  
  65. end.
  66.